package com.hxkcloud.patient.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.NurseMonitorBean;
import com.hxkcloud.patient.utils.ImageOptionUtils;
import com.hxkcloud.patient.utils.Utils;
import com.hxkcloud.patient.views.CircleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/13.
 */
public class MonitorInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<NurseMonitorBean> datas;
    private LayoutInflater mInflater;
    private ImageLoader imageLoader;
    private int headWidth = 0;
    private ResultSelectedListener listener;
    private int type;//1监测提醒医护端2评估提醒医护端(3评估提醒患者端)
    private int nowYear = 0;

    public MonitorInfoAdapter(Context mContext, List<NurseMonitorBean> datas,
                              ResultSelectedListener listener, int screenWidth, int type) {
        this.mContext = mContext;
        this.datas = datas;
        this.listener = listener;
        this.type = type;
        headWidth = screenWidth / 8;
        mInflater = LayoutInflater.from(mContext);
        imageLoader = ImageLoader.getInstance();
        nowYear = Calendar.getInstance().get(Calendar.YEAR);
    }

    public void setData(List<NurseMonitorBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void clearData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return datas.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        // TODO Auto-generated method stub


        Holder mHolder = (Holder) holder;
        NurseMonitorBean bean = datas.get(position);
        LinearLayout.LayoutParams lp_head = new LinearLayout.LayoutParams(headWidth, headWidth);
        mHolder.iv_head.setLayoutParams(lp_head);

//        imageLoader.displayImage(UrlConfig.image_host + bean.getPatientHead(), mHolder.iv_head, ImageOptionUtils.getInstance().getMan20HeadOptions());
        String age = "";
        if (!TextUtils.isEmpty(bean.getAge())) {
            int ageYear = Integer.valueOf(bean.getAge());
            age = (nowYear - ageYear) + "";
        }
        imageLoader.displayImage(bean.getPatientHead(), mHolder.iv_head, ImageOptionUtils.getInstance().getPatientHeadOptions(age, bean.getSex()));
        mHolder.iv_head.setBorderColor(ContextCompat.getColor(mContext, R.color.stroke_line));
        mHolder.iv_head.setBorderWidth(1);
        mHolder.tv_title.setText(bean.getTitle());
        mHolder.tv_name.setText(bean.getPatientName());
        String dateTime = Utils.timeStamp2Date(bean.getTime(), "yyyy-MM-dd HH:mm:ss");
        if (type == 1) {
            String date1 = dateTime.split(" ")[0];
            Date dt = new Date();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String nowTime = df.format(dt);
            long days = getDaySub(date1, nowTime);
            if (days == 0) {
                mHolder.tv_date.setText(mContext.getResources().getString(R.string.today));
            } else if (days == 1) {
                mHolder.tv_date.setText(mContext.getResources().getString(R.string.yesterday));
            } else {
                mHolder.tv_date.setText(date1);
            }
            if (bean.getIntro() != null) {
                mHolder.tv_intro.setText(dateTime.split(" ")[1] + " " + bean.getIntro());
            } else {
                mHolder.tv_intro.setText(dateTime.split(" ")[1] + " ");
            }

        } else {
            String date1 = dateTime.split(" ")[0];
            Date dt = new Date();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String nowTime = df.format(dt);
            long days = getDaySub(date1, nowTime);
            if (days == 0) {
                mHolder.tv_date.setText(mContext.getResources().getString(R.string.today));
            } else if (days == 1) {
                mHolder.tv_date.setText(mContext.getResources().getString(R.string.yesterday));
            } else {
                mHolder.tv_date.setText(date1);
            }
            mHolder.tv_intro.setText(bean.getIntro() + "已完成,请查看。");
        }
        mHolder.layout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.onSelected("layout", type, position);
            }

        });


    }

    private long getDaySub(String beginDateStr, String endDateStr) {
        long day = 0;
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
        java.util.Date beginDate;
        java.util.Date endDate;
        try {
            beginDate = format.parse(beginDateStr);
            endDate = format.parse(endDateStr);
            day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
            //System.out.println("相隔的天数="+day);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View v = mInflater.inflate(R.layout.item_monitorinfo, null);
        RecyclerView.ViewHolder holer = new Holder(v);

        return holer;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    /*
     * Holder
     */
    public class Holder extends RecyclerView.ViewHolder {

        private LinearLayout layout;
        private CircleImageView iv_head;
        private TextView tv_title;
        private TextView tv_name;
        private TextView tv_date;
        private TextView tv_intro;

        public Holder(View view) {
            super(view);
            layout = (LinearLayout) view.findViewById(R.id.tv_monitorinfo_info);
            iv_head = (CircleImageView) view.findViewById(R.id.iv_monitorinfo_head);
            tv_title = (TextView) view.findViewById(R.id.tv_monitorinfo_title);
            tv_name = (TextView) view.findViewById(R.id.tv_monitorinfo_name);
            tv_date = (TextView) view.findViewById(R.id.tv_monitorinfo_date);
            tv_intro = (TextView) view.findViewById(R.id.tv_monitorinfo_intro);
        }
    }

    public interface ResultSelectedListener {
        void onSelected(String typeStr, int type, int position);
    }


}
